
$(function () {
    var ty_datagrid = $("#ty_datagrid");
    var parent_dialog = $("#parent_dialog");
    var sub_dialog = $("#sub_dialog");
    var parent_dialog = $("#parent_dialog");



    /* //alert("好人啊");
     //tabs添加选项卡
     $('#tt').tabs({
     fit:true,

     //href:加载仅仅是html中body
     //href:node.url,
     //使用内嵌的方式
     //content:"<iframe width='98%'  height='98%'  src="+node.url+"></iframe>",
     closable:true
     })*/

    //员工列表初始化
    ty_datagrid.datagrid({
        fit:true,
        url:"/types/list",
        fitColumns:true,
        rownumbers:true,
        //分页
        pagination:true,
        pageList:[3,5,10],
        striped:true,
        singleSelect:true,
        fit:"true",

        columns:[[
            /*  // {field:"ck",checkbox:true},  //显示多选框样式
             {field:"id", title:"编号", hidden:true},
             {field:"category", title:"支出分类", width:100},
             {field:"amount", title:"支出金额", width:100},
             {field:"date", title:"支出时间", width:100},
             {field:"staff", title:"支出职员", width:100},
             {field:"remark", title:"备注", width:100},*/
        ]],
        toolbar:"#bb",
    })

    //编辑或更新对话框
    exp_dialog.dialog({
        width:300,
        height:300,
        buttons:"#vv",
        closed:true
    })


    //响应函数集合对象
    var cmdObj = {
        //工具栏的刷新
        reload: function (){
            ty_datagrid.datagrid("load");
        },

        //工具栏的编辑
        edit: function (){
            //alert("save");
            //判断是否选中数据
            var row =  ty_datagrid .datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择要编辑的数据");
                return;
            }
            //添加与编辑时将数据清空
            $("#emp_form").form("clear");

            //数据显: 同名原则
            $("#emp_form").form("load", row);
            //1:弹框
            exp_dialog.dialog("open");
            //2:设置标题
            exp_dialog.dialog("setTitle", "支出编辑");
        },
        //工具栏添加
        add:function (){
            //添加与编辑时将数据清空
            $("#emp_form").form("clear");
            //1:弹框
            exp_dialog.dialog("open");
            //2:设置标题
            exp_dialog.dialog("setTitle", "支出录入");

        },

        //保存操作
        save:function () {
            var id = $("#id").val();
            //alert(id);
            var url =  "/type/save";
            if(id){
                url =  "/types/update";
            }
            $("#emp_form").form('submit', {
                url: url,
                success: function (data) {
                    data = $.parseJSON(data);

                    if(!data.success){
                        $.messager.alert('温馨提示',data.msg);

                    }else{
                        //关闭框
                        exp_dialog.dialog("close");
                        //刷新列表数据
                        ty_datagrid.datagrid("load");
                    }
                }
            })
        },

        //删除操作
        remove:function () {

            //判断是否选中数据
            var row =  ty_datagrid .datagrid("getSelected");
            if(!row){
                $.messager.alert('温馨提示',"请选择要删除的数据");
                return;
            }
            //1:发起请求修改状态
            $.get("/expenditure/delete",{id:row.id}, function(data){

                if(data.success){
                    //2:刷新列表数据
                    ty_datagrid.datagrid("load");
                }else{
                    $.messager.alert('温馨提示',data.msg);
                }
            })

        },

        cancel:function (){
            exp_dialog.dialog("close");
        },

        //工具栏的高级查询
        query: function(){

            var bD = $("#begin").datebox("getValue");
            var eD = $("#end").datebox("getValue");

            ty_datagrid.datagrid("load", {
                beginDate:bD,
                endDate:eD
            });
        }
    }

    //调用
    $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");//eidt,remove,relod
        cmdObj[cmd]();
    })



    function appendSpan() {
        alert("appendSpan")
        var span = document.getElementById("span");
        var div1 = document.getElementById("div1");
        // 将Span追加到div1中
        div1.appendChild(span);
    }

    function appendNewSpan() {
        alert("appendNewSpan")
        // <span></span>
        var newSpan = document.createElement("span");
        // 为span设置文本
        newSpan.innerHTML = "newSpan";
        var div1 = document.getElementById("div1");
        div1.appendChild(newSpan);
    }

    //父元素.removeChild(子元素)
    function deleteSpan() {
        alert("deleteSpan")
        //var div1 = document.getElementById("div1");
        var span = document.getElementById("span");
        //div1.removeChild(span);
        //根据子元素找到父元素,然后在删除子元素
        span.parentNode.removeChild(span);
    }

    function replaceSpan(){
        alert("replaceSpan")
        var newSpan = document.createElement("span");
        newSpan.innerHTML = "newSpan";
        var span = document.getElementById("span");
        var div1 = document.getElementById("div1");
        div1.replaceChild(newSpan,span);
    }
    function insertSpan(){

        alert("insertSpan")
        var newSpan = document.createElement("span");
        newSpan.innerHTML = "newSpan";
        var span = document.getElementById("span");
        var div1 = document.getElementById("div1");
        div1.insertBefore(newSpan, span);
    }

    function getNode(){

        alert("getNode")
        var div1 = document.getElementById("div1");
        //获取div1中的第一个子元素
        console.log(div1.firstChild);
        console.log(div1.lastChild);
        if(div1.hasChildNodes()){
            console.log(div1.childNodes);
        }
        console.log(div1.previousSibling);
        console.log(div1.attributes[0]);
    }
});





